package http

import (
	"server/app/utils"

	"github.com/gin-gonic/gin"
)

func Login(c *gin.Context) {
	type r struct {
		Username string `json:"username" binding:"required"`
		Password string `json:"password" binding:"required"`
	}
	arg := &r{}
	err := c.ShouldBind(arg)
	if err != nil {
		utils.Failed(c, err)
		return
	}

	token, err := userSvc.Login(arg.Username, arg.Password, c.GetHeader("User-Agent"))
	if err != nil {
		utils.Failed(c, err)
		return
	}

	utils.Success(c, gin.H{
		"token": token,
	})
}

func Register(c *gin.Context) {
	type r struct {
		Username string `json:"username" binding:"required"`
		Password string `json:"password" binding:"required"`
	}
	arg := &r{}
	err := c.ShouldBind(arg)
	if err != nil {
		utils.Failed(c, err)
		return
	}

	err = userSvc.Register(arg.Username, arg.Password)
	if err != nil {
		utils.Failed(c, err)
		return
	}

	utils.Success(c)
}
